package com.company;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPClient {

    public static void main(String[] args) throws Exception{
        DatagramSocket ds = new DatagramSocket(8888);
        InetAddress address = InetAddress.getByName("192.168.31.149");
        while(true) {
            System.out.println("请输入要发送的信息：");
            Scanner sc = new Scanner(System.in);
            String message = sc.nextLine();
            byte[] buf = message.getBytes();
            DatagramPacket dp = new DatagramPacket(buf,buf.length,address,4603);
            System.out.println("发送信息");
            ds.send(dp);

            byte[] buf2 = new byte[1024];
            DatagramPacket fromServerPacket = new DatagramPacket(buf2,buf2.length);
            System.out.println("等待服务端响应……");
            ds.receive(fromServerPacket);
            String response = new String(fromServerPacket.getData(),0,fromServerPacket.getLength());
            System.out.println("从服务端接收到信息:" + response);
            if(response.equals("Exit"))
                break;
        }
        ds.close();
    }
}
